package ua.maxicash.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ua.maxicash.dao.CalendarDao;
import ua.maxicash.vo.*;

import java.util.Date;
import java.util.List;
import java.util.Set;

@Service("CalendarService")
public class CalendarServiceImpl implements CalendarService {

    @Autowired
    CalendarDao calendarDao;

    @Override
    public List<Date> getCalendarForMonth(int month, int year, SystemUser su) {

        if (su instanceof Worker) {
            return calendarDao.getWorkersCalendarForMonth(month, year, su.getIdUser());
        } else {
            return calendarDao.getEmployersCalendarForMonth(month, year, su.getIdUser());
        }

    }

    @Override
    public Set<WorkingDay> getCalendarForDay(int day, int month, int year, SystemUser su) {

        if (su instanceof Worker) {
            return calendarDao.getWorkersCalendarForDay(day, month, year, su.getIdUser());
        } else {
            return calendarDao.getEmployersCalendarForDay(day, month, year, su.getIdUser());
        }
    }

    @Override
    public Set<Work> getNearestWorks(SystemUser su, int amount) {

        Date now = new Date(System.currentTimeMillis());
        if (su instanceof Worker) {
            return calendarDao.getNearestWorkerWorksForData(now, amount, su.getIdUser());
        }
        if (su instanceof Employer) {
            return calendarDao.getNearestEmployerWorksForData(now, amount, su.getIdUser());
        }
        throw new RuntimeException("System user is not instance of Worker neither Employer");

    }

    @Override
    public Set<Work> getRandomWorks(SystemUser su, int amount) {

        Date now = new Date(System.currentTimeMillis());
        if (su instanceof Worker) {
            return calendarDao.getNearestRandomWorksForWorker(now, amount, su.getIdUser());
        }
        // TODO check!!!
        if (su instanceof Employer) {
            //return calendarDao.getNearestRandomWorksForWorker(now, amount, su.getIdUser());
        }
        throw new RuntimeException("System user is not instance of Worker neither Employer");

    }
}
